#!/usr/bin/env python
import sys, re, time
from datetime import datetime
from model import *
from querymanager import *

totalqueries = 0
incorrect = 0

def check(qm, queryfile):
    global totalqueries
    global incorrect
    print '  ',queryfile
    queries = open('%s.txt'%queryfile,'r')
    answers = open('%s_expected.txt'%queryfile,'r')
    correct = 0
    total = 0
    for q in queries:
        totalqueries += 1
        total += 1
        t = now()
        query = qm.qp.parse(str(q))
        r = query.execute(index)
        if 'part2' in queryfile:
            scorer = query.scorer(index)
            results = ','.join(map(str,r.limit(10).orderWith(scorer).all()))
        else:
            results = ','.join(map(str,r.all()))
        ans = answers.readline()
        #ansset = set(ans.replace('\n','').split(','))
        ansset = ans.replace('\n','')
        #res = results == ansset or results==set([]) and ansset == set([''])
        res = results == ansset
        correct += int(res)
        if not res:
            incorrect += 1
            print '\t\t', res, q.replace('\n','')
            print results
            print ansset#, results - ansset, ansset - results
        #else:
        print '    ', res, q.replace('\n','')
        print '      runtime:',now() - t
    print 'correct: %d/%d'%(correct, total)
        
if __name__ == "__main__":
    arg = sys.argv[1:]
    if len(arg) > 0:
        skip = arg[0]
    else:
        skip = ''
    try:
        import psyco
        psyco.full()
    except:
        pass
    docs = {"collections/seasons":
                ["queries/part1/bool_queries_season",
                 "queries/part1/oneword_queries_season",
                 "queries/part1/phrase_queries_season",
                 "queries/part2/bool_queries_season",
                 "queries/part2/ftq_queries_seasons",
                 "queries/part2/oneword_queries_season",
                 "queries/part2/oneword_queries_seasons",
                 "queries/part2/phrase_queries_season",
                 "queries/part2/bool_queries_seasons",
                 "queries/part2/phrase_queries_seasons"]
            ,
            "collections/blogs":
                ["queries/part2/bool_queries_blogs",
                 "queries/part2/phrase_queries_blogs"]
            ,
            "collections/wiki_math":
                ["queries/part1/bool_queries_math",
                 "queries/part1/oneword_queries_math",
                 "queries/part1/phrase_queries_math",
                 "queries/part1_test/bonus_test_queries_math",
                 "queries/part1_test/bool_test_queries_math",
                 "queries/part1_test/oneword_test_queries_math",
                 "queries/part1_test/phrase_test_queries_math"]
            ,
            "collections/wiki_tennis":
                ["queries/part1/bool_queries_tennis",
                 "queries/part1/oneword_queries_tennis",
                 "queries/part1/phrase_queries_tennis",
                 "queries/part1_test/bonus_test_queries_tennis",
                 "queries/part1_test/bool_test_queries_tennis",
                 "queries/part1_test/oneword_test_queries_tennis",
                 "queries/part1_test/phrase_test_queries_tennis"]
            ,
            "collections/wiki_mt":
                ["queries/part2/bool_queries_mt",
                 "queries/part2/ftq_queries_mt",
                 "queries/part2/oneword_queries_mt",
                 "queries/part2/phrase_queries_mt"]
            }

    t1 = now()
    for k,v in docs.items():
        if skip:
            if k != "collections/%s"%skip: continue
        t2 = now()
        index = Index.fromFile('%s_dictionary.txt'%k, '%s_postings.txt'%k, 1)
        qm = QueryManager(index)
        print k
        print 'read index time:', now()-t2
        for i in v:
            check(qm, i)
    print '%d queries executed in total time:'%totalqueries, now() - t1
    print 'incorrect: %d of %d'%(incorrect, totalqueries)